package com.AAL.DAO;

import java.sql.Timestamp;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.classic.Session;

import com.AAL.DAO.Interface.IPurchaseInvoiceDAO;
import com.AAL.Model.PurchaseInvoice;
import com.AAL.Model.StatusOfInvoice;
import com.AAL.Utils.HibernateUtil;

public class PurchaseInvoiceDAO implements IPurchaseInvoiceDAO {

	
	// Create and Update
	@Override
	public PurchaseInvoice saveAndUpdatePurchaseInvoice(
			PurchaseInvoice PurchaseInvoice) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		try {
			session.saveOrUpdate(PurchaseInvoice);
		} catch (Exception e) {
			return null;
		}
		session.getTransaction().commit();
		return PurchaseInvoice;
	}
	//delete PurchaseInvoice
	@Override
	public PurchaseInvoice deletePurchaseInvoice(int purchaseInvoiceID) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		try {
			session.beginTransaction();
			PurchaseInvoice purchaseInvoice = (PurchaseInvoice) session.load(
					PurchaseInvoice.class, purchaseInvoiceID);
			if (null != purchaseInvoice) {
				session.delete(purchaseInvoice);
			}
			session.getTransaction().commit();
			return purchaseInvoice;
		} catch (Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
			return null;
		}

	}
	//Get PurchaseInvoice
	@Override
	public PurchaseInvoice getPurchaseInvoice(String purchaseInvoiceID) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		PurchaseInvoice purchaseInvoice = (PurchaseInvoice) session.load(
				PurchaseInvoice.class, purchaseInvoiceID);
		return purchaseInvoice;
	}
	//Get List PurchaseInvoice by PurchaseConstractID
	@SuppressWarnings("unchecked")
	@Override
	public List<PurchaseInvoice> getLstPurchaseInvoicesByPurchaseContract(int purchaseContractID) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		
		List<PurchaseInvoice> purchaseInvoices = null;
		try {
			session.beginTransaction();
			
			
			purchaseInvoices=(List<PurchaseInvoice>)session.createQuery("from PurchaseInvoice where purchaseContract.purchaseContractID=:purchaseID").setParameter("purchaseID", purchaseContractID).list();
			
			session.getTransaction().commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
			return null;
		}
		
		return purchaseInvoices;
	}

	//get list all purchaseContract
	
	@SuppressWarnings("unchecked")
	@Override
	public List<PurchaseInvoice> getLstPurchaseInvoices() {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		
		List<PurchaseInvoice> purchaseInvoices = null;
		try {
			session.beginTransaction();
			purchaseInvoices=(List<PurchaseInvoice>)session.createQuery("from PurchaseInvoice").list();
			session.getTransaction().commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
			return null;
		}
		
		return purchaseInvoices;
	}


	

	@Override
	public List<PurchaseInvoice> getLstPurchaseInvoicesByStatus(
			StatusOfInvoice status) {
		// TODO Auto-generated method stub
		return null;
	}
	/*
	 * Search PurchaseInvoice
	 * @see com.AAL.DAO.Interface.IPurchaseInvoiceDAO#searchPurchaseInvoices(java.lang.String, java.sql.Timestamp, java.sql.Timestamp)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<PurchaseInvoice> searchPurchaseInvoices(String status,
			Timestamp begin, Timestamp end) {
		Session session=HibernateUtil.getSessionFactory().getCurrentSession();
		List<PurchaseInvoice> purchaseInvoices = null;
		try{
			session.beginTransaction();
			purchaseInvoices=(List<PurchaseInvoice>) session.createQuery
			("from PurchaseInvoice where (statusOfInvoice.statusName=:status) and (writingDate between :begin and :end)")
			.setParameter("status",status)
			.setParameter("begin", begin)
			.setParameter("end",end).list();
			session.getTransaction().commit();
		}
		catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		return purchaseInvoices;
	}

}
